package sv.ues.fia.adminperfildocentes;

import sv.ues.fia.adminperfildocentes.model.Escalafon;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class ConsultarEscalafonActivity extends Activity {
	
	ControlDB helper;
	EditText editId;
	EditText editCod;
	EditText editNombre;
	String id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_consultar_escalafon);
		helper = new ControlDB(this);
		editId = (EditText) findViewById(R.id.editIdEscalafon);
		editCod = (EditText) findViewById(R.id.editCodEscalafon);
		editNombre = (EditText) findViewById(R.id.editNomEscalafon);
		
		Bundle bundle = getIntent().getExtras();
		id = String.valueOf(bundle.getLong("idEscalafon"));
		
		helper.abrir();
		Escalafon escalafon = helper.consultarEscalafon(id);
		helper.cerrar();
		if(escalafon == null)
		{
			Toast.makeText(this, getResources().getString(R.string.escalafon_con) +
					id + getResources().getString(R.string.no_encontrado), Toast.LENGTH_LONG).show();
		}
		else
		{
			editId.setText(String.valueOf(escalafon.getIdescalafon()));
			editCod.setText(escalafon.getCodescalafon());
			editNombre.setText(escalafon.getNombre());
		}
	}

	public void llamarActivityActualizar(View v)
	{
		Intent intent = new Intent(getApplicationContext(), ActualizarEscalafonActivity.class);
		intent.putExtra("idEscalafon", id);
		startActivity(intent); 
	}
	
	public void eliminarEscalafon(View v)
	{
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
				this);
 
			// set title
			alertDialogBuilder.setTitle(R.string.eliminareg);
 
			// set dialog message
			alertDialogBuilder
				.setMessage(R.string.msjeliminar)
				.setCancelable(false)
				.setPositiveButton(R.string.si,new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int idOption) {
						
						Escalafon escalafon = new Escalafon();
						escalafon.setIdescalafon(Integer.parseInt(id));
						helper.abrir();
						int accion = helper.eliminarEscalafon(escalafon);
						helper.cerrar();
						if (accion == -1 || accion == 0) {
							Toast.makeText(getBaseContext(), R.string.erroreliminar, Toast.LENGTH_SHORT).show();
							Intent intent1 = new Intent(getApplicationContext(), ListarEscalafonActivity.class);
							startActivity(intent1);
						} else {
							Toast.makeText(getBaseContext(), R.string.okeliminar, Toast.LENGTH_SHORT).show();
							Intent intent2 = new Intent(getApplicationContext(), ListarEscalafonActivity.class);
							startActivity(intent2);
						}
					
						
					}
				  })
				.setNegativeButton(R.string.no,new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int id) {
						// if this button is clicked, just close
						// the dialog box and do nothing
						dialog.cancel();
					}
				});
 
				// create alert dialog
				AlertDialog alertDialog = alertDialogBuilder.create();
 
				// show it
				alertDialog.show();


	}

}
